package com.itboye.textileexpres.util;

import com.itboye.textileexpres.R;
import com.itboye.textileexpres.main.MainActivity;
import com.squareup.picasso.Picasso;
import com.squareup.picasso.Transformation;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.Log;
import android.widget.ImageView;

public class XPicasso {

	public static void loadImage(Context context, ImageView imageView,
			String url) {
		Picasso.with(context).load(url).placeholder(R.drawable.ic_launcher)
				.error(R.drawable.ic_launcher).transform(transformation)
				.into(imageView);
	}

	static Transformation transformation = new Transformation() {

		@Override
		public Bitmap transform(Bitmap source) {

			int targetWidth = MainActivity.WIDTH;
			Log.i("width", "source.getHeight()=" + source.getHeight()
					+ ",source.getWidth()=" + source.getWidth()
					+ ",targetWidth=" + targetWidth);

			// if(source.getWidth()==0){
			// return source;
			// }
			//
			// //如果图片小于设置的宽度，则返回原图
			// // if(source.getWidth()<targetWidth){
			// // return source;
			// // }else{
			// //如果图片大小大于等于设置的宽度，则按照设置的宽度比例来缩放
			// double aspectRatio = (double) source.getHeight() / (double)
			// source.getWidth();
			// int targetHeight = (int) (targetWidth * aspectRatio);
			// if (targetHeight != 0 && targetWidth != 0) {
			// Bitmap result = Bitmap.createScaledBitmap(source, targetWidth,
			// targetHeight, false);
			// if (result != source) {
			// // Same bitmap is returned if sizes are the same
			// source.recycle();
			// }
			// return result;
			// } else {
			return source;
			// }
			// }

		}

		@Override
		public String key() {
			return "transformation" + " desiredWidth";
		}
	};
}
